package cn.jdy.xyajj.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.xyajj.entity.XyajjCompany;

/**
 * @title  襄阳市安全生产监督管理局-企业信息管理操作
 * @author ningqian
 * @date   2017-2-22 11:24:00
 */
@Service
public class XyajjCompanyService extends BaseDao<XyajjCompany>{

	/**
	 * 按条件查询
	 * @param company
	 * @return
	 */
	public List<XyajjCompany> query(XyajjCompany company,String... date){
		if(company==null) return super.findAll();
		if(company.getiDisplayLength() > 0){
	     return super.find(createFilter(company),company.getiDisplayStart(),company.getiDisplayLength());
		}
		return super.find(createFilter(company));
	}
	
    
	public Long count(XyajjCompany company,String... date){
		return super.count(createFilter(company));
	}
	
	/**
	 * 拼装条件
	 * @param company
	 * @return
	 */
	private DetachedCriteria createFilter(XyajjCompany company, String... date){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(company.getCompanyName())) {
	        dc.add(Restrictions.like("companyName", company.getCompanyName().trim(),MatchMode.ANYWHERE));
		} 
		if(!StringUtil.isNullOrEmpty(company.getBusinessNum())) {
	        dc.add(Restrictions.eq("businessNum", company.getBusinessNum()));
		} 
	  
		dc.addOrder(Order.desc("createTime"));
		dc.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);// 结果集去重
		return dc;
	}
	

}
